package com.hengxinguotong.hxgtwygsp.ui.main.video;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.media.MediaPlayer;
import android.net.Uri;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.View;

import com.hengxinguotong.hxgtwygsp.R;
import com.hengxinguotong.hxgtwygsp.animator.TranslationBuilder;
import com.hengxinguotong.hxgtwygsp.model.PageEvent;
import com.hengxinguotong.hxgtwygsp.model.Video;
import com.hengxinguotong.hxgtwygsp.utils.CommUtil;
import com.hengxinguotong.hxgtwygsp.utils.TimeDown;
import com.trello.rxlifecycle2.LifecycleTransformer;
import com.trello.rxlifecycle2.android.FragmentEvent;

import org.greenrobot.eventbus.EventBus;

import java.io.File;
import java.io.IOException;

import javax.inject.Inject;

/**
 * TODO
 *
 * @author hjz
 * @version V1.0.0
 * @since 2019/3/23 13:38
 */

public class VideoPresenter implements VideoContract.IPresenter, MediaPlayer.OnCompletionListener
{
    private Context context;
    private VideoContract.IView mView;
    private MediaPlayer mediaPlayer;

    private ObjectAnimator dismissAnimator;
    private ObjectAnimator showAnimator;

    private TimeDown handerTimeDown;

    private Uri videoUri = null;

    @Inject
    public VideoPresenter(Context context)
    {
        this.context = context;
        videoUri = Uri.parse(CommUtil.getDataDir(context) + File.separator + "0.mp4");
    }

    @Override
    public void bindView(VideoContract.IView iView)
    {
        this.mView = iView;
        LifecycleTransformer<Long> transformer = ((VideoFragment) mView).bindUntilEvent(FragmentEvent.DESTROY_VIEW);
        handerTimeDown = new TimeDown(transformer, () ->
        {
            mView.updateHanderStatus();
            dismissAnimator();
        });
        handerTimeDown.startTimer();
    }

    @Override
    public void dropView()
    {
        if (mediaPlayer != null)
        {
            mediaPlayer.release();
        }
        this.mView = null;
    }

    @Override
    public void initMediaPlayer(SurfaceHolder holder)
    {
        mediaPlayer = new MediaPlayer();
        mediaPlayer.setDisplay(holder);
        mediaPlayer.setOnCompletionListener(this);
    }

    @Override
    public void prepare(Video video)
    {
        mediaPlayer.reset();
        try
        {
            videoUri = Uri.parse(CommUtil.getDataDir(context) + File.separator + video.getName());
            mediaPlayer.setDataSource(context, videoUri);
            mediaPlayer.prepare();
        } catch (IOException e)
        {
            e.printStackTrace();
        }
    }

    @Override
    public void pause()
    {
        if (mediaPlayer != null && mediaPlayer.isPlaying())
        {
            mediaPlayer.pause();
        }
    }

    @Override
    public void start()
    {
        if (mediaPlayer != null)
        {
            mediaPlayer.start();
        }
    }

    @Override
    public void initAnimator(View target)
    {
        float value = context.getResources().getDimensionPixelSize(R.dimen.px_182);
        dismissAnimator = new TranslationBuilder()
                .buildTarget(target)
                .buildPropertyName("translationX")
                .buildValues(0f, value)
                .buildDuration(400)
                .create();
        showAnimator = new TranslationBuilder()
                .buildTarget(target)
                .buildPropertyName("translationX")
                .buildValues(value, 0f)
                .buildDuration(400)
                .create();
    }

    @Override
    public void dismissAnimator()
    {
        dismissAnimator.start();
    }

    @Override
    public void showAnimator()
    {
        showAnimator.start();
        handerTimeDown.startTimer();
    }

    @Override
    public void recoverRemainTime()
    {
        handerTimeDown.recoverRemainTime();
    }

    @Override
    public void onCompletion(MediaPlayer mp)
    {
        Log.e("TAG===", "play over----");
        mView.updateCompleted();
        EventBus.getDefault().post(new PageEvent());
    }

}
